package sqlist;

import java.util.Arrays;

public class MyArraylist {

    public int[] elem;
    public int usedSize;//0
    private static final int DEFAULT_SIZE = 8;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    //打印顺序表
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    //新增元素，默认在数组最后新增
    public void add(int data) {
        //满了，进行扩容
        if(this.isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2*this.elem.length);
        }
        //不满，进行插入
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    /**
     * 判断当前顺序表是不是满的
     * @return true:满  false:不满
     */
    public boolean isFull() {
//        if(this.elem.length == usedSize) {
//            return true;
//        }
//        return false;
        return this.elem.length == usedSize;
    }

    private boolean checkPosInAdd(int pos) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("pos位置不合法");
            return false;
        }
        return true;//合法
    }

    //在pos位置上新增元素
    public void add(int pos, int data) {
        if(!checkPosInAdd(pos)) {
            throw new MyArrayListIndexOutOfException("添加方法的pos不合理");
        }
        //满了，进行扩容
        if(this.isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2*this.elem.length);
        }
        //不满，进行插入
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    //判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean checkPosInGet(int pos) {
        if(pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法");
            return false;
        }
        return true;//合法
    }

    //获取 pos位置的元素
    public int get(int pos) {
        if(!checkPosInGet(pos)) {
            throw new MyArrayListIndexOutOfException("获取pos下标时，位置不合法");
        }

        if(isEmpty()) {
            throw new MyArrayListEmptyException("获取元素的时候，顺序表为空");
        }

        return this.elem[pos];
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }

    //给pos位置的元素更新为value
    public void set(int pos, int value) {
        if(!checkPosInGet(pos)) {
            throw new MyArrayListIndexOutOfException("更新pos下标的元素，位置不合法");
        }

        if(isEmpty()) {
            throw new MyArrayListEmptyException("顺序表为空");
        }

        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int key) {
        if(isEmpty()) {
            throw new MyArrayListEmptyException("顺序表为空，不能删除");
        }

        int index = indexOf(key);

        if(index == -1) {
            System.out.println("不存在你要删除的数据");
            return;
        }

        for (int i = index; i < this.usedSize - 1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
        //this.elem[usedSize] = 0;
    }

    //获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    //清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}
